package com.ark.controller.admin;

import com.ark.constant.JwtClaimsConstant;
import com.ark.dto.EmployeeDTO;
import com.ark.dto.EmployeeLoginDTO;
import com.ark.dto.EmployeePageQueryDTO;
import com.ark.dto.PasswordEditDTO;
import com.ark.entity.Employee;
import com.ark.properties.JwtProperties;
import com.ark.result.PageResult;
import com.ark.result.Result;
import com.ark.service.EmployeeService;
import com.ark.utils.JwtUtil;
import com.ark.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Tag(name = "员工管理",description = "提供员工相关的操作接口")
public class EmployeeController {

    private final JwtProperties jwtProperties;
    private final EmployeeService employeeService;

    public EmployeeController(JwtProperties jwtProperties,EmployeeService employeeService) {
        this.jwtProperties = jwtProperties;
        this.employeeService = employeeService;
    }


    @Operation(summary = "登录",description = "根据用户名密码执行登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录: {}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims
        );

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();


        return Result.success(employeeLoginVO);
    }


    @Operation(summary = "登出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    @PostMapping
    @Operation(summary = "新增员工")
    public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工: {}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    @Operation(summary = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult<Employee>> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询: {}", employeePageQueryDTO);
        PageResult<Employee> pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping("/status/{status}")
    @Operation(summary = "启用禁用员工账号")
    @Parameters({
            @Parameter(name = "status",description = "状态设置",in = ParameterIn.PATH),
            @Parameter(name = "id",description = "影响用户id",in = ParameterIn.QUERY)
    })
    public Result<String> startOrStop(@PathVariable("status") Integer status,Long id) {
        log.info("启用禁用员工账号: {},{}", status, id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    @Operation(summary = "根据id查询员工信息")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") @Parameter(description = "员工id") Long id) {
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @Operation(summary = "编辑员工信息")
    public Result<?> update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息: {}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @Operation(summary = "修改密码")
    @PutMapping("/editPassword")
    public Result<?> editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        log.info("员工修改密码: {}", passwordEditDTO);
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }


}
